package com.kl.component.spring.event;


import com.alibaba.fastjson.JSON;
import com.kl.component.spring.tool.SpringApplicationContextTool;
import com.kl.modular.sysman.system.service.api.ISystemAreaCacheService;
import com.kl.modular.worm.service.api.IWormGrabedPageService;
import com.kl.modular.worm.service.api.IWormHomePageService;
import com.kl.modular.worm.service.api.IWormPageRegularService;
import com.kl.modular.worm.service.api.IWormPendPageService;
import com.worms.crawler.api.IDefaultCrawlerCallback;
import com.worms.crawler.model.CrawlerRspInfo;
import com.worms.crawler.tactis.BfsCrawler;
import com.worms.http.util.HtmlResponseUtil;
import com.worms.http.util.IpUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.conn.params.ConnRouteParams;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DecompressingHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;


/**
 *
 * 加载初始化
 *
 * @author xr_zjb
 * @time 2016/6/19 14:08
 *
 */
public class InstantiationTracingBeanPostProcessor implements ApplicationListener<ContextRefreshedEvent>{

	private static final Logger logger = LoggerFactory.getLogger(InstantiationTracingBeanPostProcessor.class);

	public void onApplicationEvent(ContextRefreshedEvent event) {
		if (event.getApplicationContext().getParent() == null) {
			logger.info("爬虫任务加载中...");
			IWormPendPageService wormPendPageService = (IWormPendPageService)SpringApplicationContextTool.getBean("wormPendPageService");
			IWormGrabedPageService wormGrabedPageService = (IWormGrabedPageService)SpringApplicationContextTool.getBean("wormGrabedPageService");
			IWormPageRegularService wormPageRegularService = (IWormPageRegularService)SpringApplicationContextTool.getBean("wormPageRegularService");
			IWormHomePageService wormHomePageService = (IWormHomePageService)SpringApplicationContextTool.getBean("wormHomePageService");
			HashMap<String, Object> dataObjMap = new HashMap<String, Object>();
			dataObjMap.put("wormPendPageService", wormPendPageService);
			dataObjMap.put("wormGrabedPageService", wormGrabedPageService);
			dataObjMap.put("wormPageRegularService", wormPageRegularService);
			dataObjMap.put("wormHomePageService", wormHomePageService);

			BfsCrawler bfsCrawler = new BfsCrawler(new IDefaultCrawlerCallback() {
				private int nums = 0;
				public void grabResponse(CrawlerRspInfo rspInfo) {
					nums++;
					logger.info(nums + " : " + rspInfo.getUrl());

                    // 迅雷资源正则匹配
                    List<String> thunderList = findThunderInfo(rspInfo.getContent(), null);
                    //System.out.println(JSON.toJSONString(thunderList));
                    // 标题获取

                    Document doc = Jsoup.parse(rspInfo.getContent());
                    Elements fontTitle = doc.select("font[color=#07519a]");
                    String title = fontTitle.get(0).text();
                    System.out.println(title);
                }
			});
			bfsCrawler.setDataObjMap(dataObjMap);
			bfsCrawler.addHomePage("http://www.dytt8.net/");
            bfsCrawler.addHomePage("http://www.dytt8.net/html/gndy/dyzz/index.html");
            bfsCrawler.addHomePage("http://www.ygdy8.net/html/gndy/oumei/index.html");
			bfsCrawler.addSeed("(http[s]{0,1}://www.dytt8.net){0,1}/html/gndy/[a-z]{1,}/list[_]{0,1}[0-9]{0,}_[0-9]{0,}.html");
            bfsCrawler.addSeed("(http[s]{0,1}://www.ygdy8.net){0,1}/html/gndy/[a-z]{1,}/list[_]{0,1}[0-9]{0,}_[0-9]{0,}.html");
			bfsCrawler.addRegular("(http[s]{0,1}://www.dytt8.net){0,1}/html/gndy/[a-z]{1,}/[0-9]{7,}/[0-9]{0,}.html");
            bfsCrawler.addRegular("(http[s]{0,1}://www.ygdy8.net){0,1}/html/gndy/[a-z]{1,}/[0-9]{7,}/[0-9]{0,}.html");
			bfsCrawler.setThreadNum(10);
			bfsCrawler.setCrawlerName(IpUtils.getIp());
			bfsCrawler.init();
			bfsCrawler.execute();
			logger.info("爬虫任务加载完成！");
		}
	}

    /**
     * 寻找匹配迅雷地址
     * @param rspContents html响应内容
     * @param thunderRegular 正则
     */
	public List<String> findThunderInfo(String rspContents, String thunderRegular){
        if(StringUtils.isEmpty(rspContents)){
            return Collections.EMPTY_LIST;
        }
        if(StringUtils.isEmpty(thunderRegular)){
            thunderRegular = "thunder://[A-Za-z0-9_]{0,}==";
        }
        List<String> regularList = new ArrayList<String>();
        regularList.add(thunderRegular);
        List<String> thunderList = HtmlResponseUtil.handleUrl(rspContents, regularList);
        return thunderList;
    }

    @Test
    public void testurl(){
        List<String> regularList = new ArrayList<String>();
        regularList.add("(http[s]{0,1}://www.dytt8.net){0,1}/html/gndy/[a-z]{1,}/list[_]{0,1}[0-9]{0,}_[0-9]{0,}.html");
        regularList.add("(http[s]{0,1}://www.ygdy8.net){0,1}/html/gndy/[a-z]{1,}/list[_]{0,1}[0-9]{0,}_[0-9]{0,}.html");
        Boolean aBoolean = HtmlResponseUtil.panIsPatternUrl("http://www.ygdy8.net/html/gndy/oumei/list_7_2.html", regularList);
        System.out.println(aBoolean);
    }

    @Test
    public void testHttp() throws IOException {

            HttpClient httpclientOld = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            CookieStore cookies = new BasicCookieStore();
            localContext.setAttribute(ClientContext.COOKIE_STORE, cookies);
            DecompressingHttpClient httpclient = new DecompressingHttpClient(httpclientOld);
            HttpGet httpget = new HttpGet("http://www.google.com");
            RequestConfig configs = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000)
                    .setCookieSpec(CookieSpecs.STANDARD_STRICT).build(); // 设置超时及cookie策略
            httpget.setConfig(configs);
            httpclient.getParams().setParameter("http.protocol.single-cookie-header", true);
            httpclient.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
            HttpHost proxy = null;

//            httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);
//            HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 5000);
//            HttpConnectionParams.setSoTimeout(httpclient.getParams(), 5000);
//            proxy = new HttpHost("188.191.30.115", 8080);
//            httpclient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);

            HttpResponse response = httpclient.execute(httpget, localContext);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                return;
            }
            HttpEntity entity = response.getEntity();
            String respContent = EntityUtils.toString(entity, ContentType.getOrDefault(entity).getCharset().toString()).trim();
            System.out.println(respContent);
    }

}
